/**
 * 
 */
package com.binhthuan.catechist.client.form;

import java.util.Date;

import com.binhthuan.catechist.client.ECApplication;
import com.binhthuan.catechist.client.data.ShiftDS;
import com.binhthuan.catechist.shared.Utilities;
import com.binhthuan.catechist.shared.constant.Parameter;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.HiddenItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.TimeItem;
import com.smartgwt.client.widgets.form.validator.DateRangeValidator;

/**
 * @author dttgiang
 *
 */
public class ShiftForm extends AbstractForm {

    private DateRangeValidator validator;

    public ShiftForm(ShiftDS dataSource) {
        super(dataSource);
        setMargin(10);
        setNumCols(4);
        //setCellPadding(5);

        validator = new DateRangeValidator();
        validator.setErrorMessage("conflicted date");

        HiddenItem idField = new HiddenItem(Parameter.ID);

        TextItem nameField = new TextItem(Parameter.NAME);
        nameField.setRequired(true);
        nameField.setTitle(ECApplication.getConstants().shiftName());
        nameField.setWidth(200);
        nameField.setColSpan(4);
        nameField.setWrapTitle(false);

        TextItem dayOfWeekField = new TextItem(Parameter.DAY_OF_WEEK);
        dayOfWeekField.setRequired(true);
        dayOfWeekField.setTitle(ECApplication.getConstants().dayOfWeek());
        dayOfWeekField.setWidth(200);
        dayOfWeekField.setColSpan(4);
        dayOfWeekField.setWrapTitle(false);

        TimeItem startTimeField = new TimeItem(Parameter.START_TIME);
        startTimeField.setTitle(ECApplication.getConstants().startTime());
        startTimeField.setRequired(true);

        TimeItem endTimeField = new TimeItem(Parameter.END_TIME);
        endTimeField.setTitle(ECApplication.getConstants().endTime());
        endTimeField.setRequired(true);
        endTimeField.setValidators(validator);

        ButtonItem saveButton = createSaveButton();
        saveButton.setColSpan(4);

        setFields(idField, nameField, dayOfWeekField, startTimeField, endTimeField, saveButton);
    }

   // @Override
    public void saveClick1() {
        if (getStartTime().compareTo(getEndTime()) >= 0) {
            validator.setMin(getStartTime());
            validator.setMax(new Date(getStartTime().getTime() - 1));
        } else {
            validator.setMin(getStartTime());
            validator.setMax(getEndTime());
        }
        
        if(Utilities.isNullOrEmpty(getShiftName())) {
            getItem(Parameter.NAME).setValue("");
        }

        if(Utilities.isNullOrEmpty(getDayOfWeek())) {
            getItem(Parameter.DAY_OF_WEEK).setValue("");
        }

        super.saveClick();
    }

    /**
     * Lấy tên ca học.
     * @return Tên ca học.
     */
    public String getShiftName() {
       return (String) ((TextItem) getItem(Parameter.NAME)).getValue();
    }

    /**
     * Lấy ngày học trong tuần.
     * @return Ngày học trong tuần.
     */
    public String getDayOfWeek() {
       return (String) ((TextItem) getItem(Parameter.DAY_OF_WEEK)).getValue();
    }

    /**
     * Lấy giờ bắt đầu.
     * 
     * @return Giờ bắt đầu.
     */
    public Date getStartTime() {
        TimeItem item = (TimeItem) getItem(Parameter.START_TIME);
        return (Date) item.getValue();
    }

    /**
     * Lấy giờ kết thúc.
     * 
     * @return Giờ kết thúc.
     */
    public Date getEndTime() {
        TimeItem item = (TimeItem) getItem(Parameter.END_TIME);
        return (Date) item.getValue();
    }
}
